import request, { defaultCacheAge } from '@/utils/request'
import type { WorkFlowDesignInfo, WorkflowDesignRow } from '@/types/custom'
import { defaultValueToJson } from '@/utils'
import { clone, findTree } from 'xe-utils'
import { defaultCol, formComponents, setFormComponents } from '@/utils/custom'

/**
 * mis接口
 * @method GetWorkFlowRole 获取所有项目列表
 */
export function useWfApi() {
  return {
    /**
     * 获取角色列表
     * @param params
     */
    GetWorkFlowRole: async (params?: object) => {
      return request<{ id: string; name: string }[]>({
        url: '/api-wf/WorkFlowRole/list',
        method: 'get',
        params,
        cacheAge: defaultCacheAge
      })
    },
    /**
     * 设置审批角色列表
     * @param data
     */
    WorkFlowRoleSetUsers: async (data: { id: string; rows: string[] }) => {
      return request<{ id: string; name: string }[]>({
        url: `/api-wf/WorkFlowRole/set_users/${data.id}`,
        method: 'put',
        data
      })
    },
    /**
     * 查询所有流程
     * @param data
     */
    GetWorkFlowList: async (data?: object) => {
      return request<{ id: string; name: string }[]>({
        url: '/api-wf/WorkFlowList/query',
        method: 'post',
        data,
        cacheAge: defaultCacheAge
      })
    },
    /**
     * 获取流程设计列表
     * @param params
     */
    GetWorkFlowDesignList: async (params?: object) => {
      return request<WorkflowDesignRow[]>({
        url: '/api-wf/WorkFlowDesign/list',
        method: 'get',
        params,
        cacheAge: defaultCacheAge
      })
    },
    /**
     * 获取流程设计列表
     * @param id
     */
    GetWorkFlowDesign: async (id: string) => {
      return request<WorkflowDesignRow>({
        url: `/api-wf/WorkFlowDesign/get/${id}`,
        method: 'get',
        cacheAge: defaultCacheAge
      })
    },
    /**
     * 获取业务流程实体列表（用于设定流程对应的实体类型）
     */
    GetWorkFlowDesignInsNames: async (params?: object) => {
      return request<TypedAny>({
        url: '/api-wf/WorkFlowDesign/get_ins_names',
        method: 'get',
        params,
        cacheAge: defaultCacheAge
      })
    },
    /**
     * 新增/编辑 业务流程定义
     */
    WorkFlowDesignSave: async (data: WorkflowDesignRow) => {
      return request({
        url: '/api-wf/WorkFlowDesign/save',
        method: 'post',
        data
      })
    },
    /**
     * 删除 业务流程定义
     */
    WorkFlowDesignDel: async (id: string) => {
      return request({
        url: `/api-wf/WorkFlowDesign/del/${id}`,
        method: 'delete'
      })
    },

    /**
     * 获取流程详情
     */
    GetWorkFlowDesignInfo: async (id: string) => {
      return request<WorkFlowDesignInfo>({
        url: `/api-wf/WorkFlowRuing/runing_info/${id}`,
        method: 'get'
      }).then((res) => {
        // 表单默认值转JSON
        defaultValueToJson(res.data.ui_form, 'default_value')
        // 表单需要的值
        setFormComponents(res.data.ui_form?.items || [])
        // 表单
        res.data.ui_form?.items.forEach((v) => {
          // 默认值转JSON
          defaultValueToJson(v, 'set_json')
          // 表单默认值转JSON
          defaultValueToJson(v, 'default_value')
          if (!v.set_json_parse.props) {
            v.set_json_parse.props = {}
          }
          if (!v.set_json_parse.col) {
            v.set_json_parse.col = clone(defaultCol, true)
          }
        })
        return res
      })
    },
    /**
     * 获取流程下一步用户审批节点的信息（流程提交前需先调用此接口）
     */
    WorkFlowRuingNext: async (params?: object) => {
      return request({
        url: '/api-wf/WorkFlowRuing/next',
        method: 'get',
        params
      })
    },
    /**
     * 启动（提交） - 业务审批工作流 (新提交或被退回到起点的流程)
     */
    WorkFlowRuingStart: async (data?: object) => {
      return request({
        url: '/api-wf/WorkFlowRuing/start',
        method: 'post',
        data
      })
    },
    /**
     * 撤回已提交的流程
     */
    WorkFlowRuingDel: async (id?: string) => {
      return request({
        url: `/api-wf/WorkFlowRuing/del/${id}`,
        method: 'delete'
      })
    },
    /**
     * 审批 - 业务审批工作流 （审批正在执行中流程）
     */
    WorkFlowRuingApproval: async (data?: object) => {
      return request({
        url: '/api-wf/WorkFlowRuing/approval',
        method: 'post',
        data
      })
    },
    /**
     * 委派 - 将待我审批的流程委派给他人
     */
    WorkFlowRuingDelegate: async (data?: object) => {
      return request({
        url: '/api-wf/WorkFlowRuing/delegate',
        method: 'post',
        data
      })
    },
    /**
     * 变更流程状态
     */
    WorkFlowListSetStatus: async (data?: object) => {
      return request({
        url: '/api-wf/WorkFlowList/set_status',
        method: 'post',
        data
      })
    },
    /**
     * 删除 整个流程
     */
    WorkFlowListDel: async (params: object) => {
      return request({
        url: `/api-wf/WorkFlowList/del`,
        method: 'delete',
        params
      })
    },
    /**
     * 获取在线用户
     */
    GetOnlineUsers: async (params?: object) => {
      return request<OnlineUserRow[]>({
        url: `/api-wf/OnlineUsers/online_users`,
        method: 'get',
        params
      })
    },
    /**
     * 获取审批类型(附带待审批数量)
     */
    GetUserAppGroup<T = any>(params?: object) {
      return request<T>({
        url: `/api-wf/WorkFlowRuing/user_app_group`,
        method: 'get',
        params
      })
    },
    /**
     * 获取审批标签页
     */
    GetRunningTitles<T = any>(data: { rows: string[] }) {
      return request<T>({
        url: `/api-wf/WorkFlowRuing/runing_titles`,
        method: 'post',
        data
      })
    },
    /**
     * 批量审批
     */
    PostBatchApprovals<T = any>(data: { rows: any[] }) {
      return request<T>({
        url: '/api-wf/WorkFlowRuing/approvals',
        method: 'post',
        data
      })
    }
  }
}
